package cs342.uwosh.edu.QuizApp.storage;

import cs342.uwosh.edu.R;
import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

//Handles 

public class MCQuestion extends Question implements RadioGroup.OnCheckedChangeListener {
	private String answer;
	private int intAnswer;
	private String[] choices;
	private RadioGroup group;
	
	public MCQuestion(String description, ArrayList<String> choices, String answer) {
		super(description);
		
		// Do not shuffle if multiple choice
		if(answer.equals("true") || answer.equals("false")) {
			this.choices = new String[choices.size()];
			this.choices = choices.toArray(this.choices);
		} else {
			this.choices = shuffle(choices);
		}
		
		// Find answer in choices and store as index
		for(int i = 0; i < this.choices.length; i++ ) {
			if(this.choices[i].equals(answer)) {
				this.intAnswer = i;
				break;
			}
		}
		this.answer = answer;
	}

	public boolean isGroupInitialized() {
		return group != null;
	}
	
	@Override
	public void display(Activity a, LinearLayout layout) {
		
		if(!isGroupInitialized()) {
			// Create the layout for the radio buttons
			LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
	                RadioGroup.LayoutParams.WRAP_CONTENT,
	                RadioGroup.LayoutParams.WRAP_CONTENT);
			
			//Populate the radiobuttons
			group = new RadioGroup(a);
			RadioButton button;
			for(int i = 0; i < choices.length; i++ ) {
				button = new RadioButton(a);
				button.setText(choices[i]);
				button.setId(i);
				group.addView(button, i, layoutParams);
			}
			group.setOnCheckedChangeListener(this);
			group.setPadding(2, 0, 0, 0);
		}
		layout.addView(group);
	}
	
	@Override
	public void showAnswer() {
		// TODO Auto-generated method stub
		RadioButton choice = (RadioButton) group.getChildAt(group.getCheckedRadioButtonId());
		RadioButton answer = (RadioButton) group.getChildAt(intAnswer);
		
		for(int i = 0; i < group.getChildCount(); i++) {
			((RadioButton)group.getChildAt(i)).setTextColor(R.color.White);
		}
		
		answer.setTextColor(R.color.Green);
		if(choice != null && choice != answer) {
			choice.setTextColor(R.color.Red);
		}
	}
	
	@Override
	public void checkAnswer() {
		// Nothing to do
	}
	
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		setCorrect(checkedId == intAnswer);
	}
	
	private String[] shuffle(ArrayList<String> choices) {
		Random ran = new Random();
		String[] qlist = new String[choices.size()];
		int i = 0;
		while(choices.size() != 0) {
			qlist[i] = choices.remove(ran.nextInt(choices.size()));
			i++;
		}
		return qlist;
	}
	
	public String toString() {
		String str = getDescription() + " - " + answer + "\n";
		for(int i = 0; i < choices.length; i++) {
			str += choices[i] + "\n";
		}
		return str;
	}

	@Override
	public String getAnswer() {
		// TODO Auto-generated method stub
		return answer;
	}
	

}
